﻿using UnityEngine;
using System.Collections;

public class LaserSight : MonoBehaviour {

	LineRenderer ln;
	public Transform origin;
	Vector3 uv_off = Vector2.zero;
	
	

	// Use this for initialization
	void Start () {
		ln = GetComponent<LineRenderer>();
	}
	
	// Update is called once per frame
	void Update () {
	
		if(origin == null)
			return;
			
		if(origin.parent.transform.parent.GetComponent<PlayerMovement>().shoot_btn == true)
			ln.renderer.enabled = false;
		else
			ln.renderer.enabled = true;
			
		// start / end
		ln.SetPosition(0,origin.position);
		ln.SetPosition(1,transform.position);
		
		// uv-scroll
		Vector3 dir = (transform.position-origin.position);
		dir.Normalize();
		uv_off.x-=dir.y*Time.deltaTime/5f;
		uv_off.y+=dir.x*Time.deltaTime/5f;
		
		ln.material.SetTextureOffset("_MainTex",uv_off);
		
		// uv-tiling
		float dist = Vector3.Distance(transform.position,origin.position);
		ln.material.SetTextureScale("_MainTex",new Vector2(dist/10,0.1f));
	
	}
}
